﻿/**********************************************
 * 功能名称：作业中心路径定义
编写人：  杨莹
建立时间： 杨 2008年11月15日
说明：
加载页面时，列出作业中心树型目录
可点击相应的树结点，进行编辑、删除
可在当前树结点增加子节点。
 **************************************************************/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;

using System.Text;

namespace Hisun.View.CostCenterPath
{


    public partial class CostCenterPathSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Panel_QueryCenterlist.Visible = false;
                Panel_GoalCenterList.Visible = false;


            }
        }

        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            var MyQuery = (from a in db.TABC_JS_CostCenter 
                           where a.停用 != true && a.是否直接成本 != true && a.类型 != "O" && a.类型 !="X"
                           && a.作业中心名称.Contains(TextBox_NameFilter.Text)
                           orderby a.作业中心名称   
                           select a ).Skip(skipAccount).Take(10);
            GridView_CostCenter.DataSource = MyQuery;
            GridView_CostCenter.DataBind();

            if (MyQuery.Count() > 0)
            {
                Panel_QueryCenterlist.Visible = true;

            }
            else
            {
                Panel_QueryCenterlist.Visible = false;
                
            }

            Panel_GoalCenterList.Visible = false;

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
             MyQuery = (from a in db.TABC_JS_CostCenter
                        where a.停用 != true && a.是否直接成本 != true && a.类型 != "O" && a.类型 != "X"
                        && a.作业中心名称.Contains(TextBox_NameFilter.Text)
                           select a).Count();

            return MyQuery;
        }

        #region 实现翻页功能的代码段
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {

                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        { 
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }
                           
                            else
                            { pageIndx = xx - 1; }
                        
                        
                        } 
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }



        #endregion


        protected void Button_EditPath_Command(object sender, CommandEventArgs e)
        {
            string MyCenterid = e.CommandArgument.ToString();
            Response.Redirect("CostCenterSelect.aspx?centerid=" + MyCenterid);

          
        }

        protected void Button_ShowPath_Command(object sender, CommandEventArgs e)
        {
            int MySelectCostCenterid = Convert.ToInt32(e.CommandArgument);
            var MyQueryDriverList = from a in db.TABC_JS_CostCenterPathFramework
                                    join b in db.TABC_JS_CostCenter on a.目标中心ID equals b.作业中心ID
                                    join c in db.TABC_JS_CostCenter on a.作业中心ID equals c.作业中心ID
                                    join d in db.TABC_DM_CostDivers on a.成本动因ID equals d.成本动因ID
                                    where a.作业中心ID == MySelectCostCenterid
                                    select new
                                    {
                                        作业中心ID = a.作业中心ID,
                                        作业中心名称 = c.作业中心名称,
                                        目标中心ID = a.目标中心ID,
                                        目标作业中心名称 = b.作业中心名称,
                                        成本动因ID = a.成本动因ID,
                                        成本动因名称 = d.成本动因名称
                                    };
            GridView_Path.DataSource = MyQueryDriverList;
            GridView_Path.DataBind();
            if (MyQueryDriverList.Count() > 0)
            {
                Panel_GoalCenterList.Visible = true;

            }
            else
            {
                Panel_GoalCenterList.Visible = false;

            }
        }
//检查当前分配路径的合理性
        protected void Button_Check_Command(object sender, CommandEventArgs e)
        {

        }


        #region 选用代码
        //自动建立分配路径


        protected void Button_Auto_Command(object sender, CommandEventArgs e)
        {
            //var MyQueryCenters = from a in db.TABC_JS_CostCenter
            //                     where a.是否直接成本 != true && new string[] { "A", "F", "M" }.Contains(a.类型)
            //                     select a;
            //foreach (var myCenter in MyQueryCenters)
            //{
                //#region 协调中心向本层级基本生产性作业中心分配路径
                //if (myCenter.类型 == "M")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && b.类型.StartsWith("B")
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion

                //#region 水费进入各车间的水归集中心
                //if (myCenter.类型 == "A" && myCenter.成本细项ID == "SF")
                //{
                //    var myquery2s = from b in db.TABC_JS_CostCenter
                //                    where b.类型 == "F" && b.成本细项ID == "SF"
                //                    select b;

                //    foreach (var myquery2 in myquery2s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery2.作业中心ID
                //                       select c).Count();
                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery2.作业中心ID;
                //            mynewrow.成本动因ID = 19;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);


                //        }


                //    }

                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }

                //}
                //#endregion

                //#region 蒸汽进入各车间的水归集中心
                //if (myCenter.类型 == "A" && myCenter.成本细项ID == "ZQ")
                //{
                //    var myquery2s = from b in db.TABC_JS_CostCenter
                //                    where b.类型 == "F" && b.成本细项ID == "ZQ"
                //                    select b;

                //    foreach (var myquery2 in myquery2s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery2.作业中心ID
                //                       select c).Count();
                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery2.作业中心ID;
                //            mynewrow.成本动因ID = 19;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);


                //        }


                //    }

                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }

                //}
                //#endregion

                //#region 电费进入各车间的水归集中心
                //if (myCenter.类型 == "A" && myCenter.成本细项ID == "DF")
                //{
                //    var myquery2s = from b in db.TABC_JS_CostCenter
                //                    where b.类型 == "F" && b.成本细项ID == "DF"
                //                    select b;

                //    foreach (var myquery2 in myquery2s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery2.作业中心ID
                //                       select c).Count();
                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery2.作业中心ID;
                //            mynewrow.成本动因ID = 19;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);


                //        }


                //    }

                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }

                //}
                //#endregion


                //#region 空压进入各车间的水归集中心
                //if (myCenter.类型 == "A" && myCenter.成本细项ID == "KY")
                //{
                //    var myquery2s = from b in db.TABC_JS_CostCenter
                //                    where b.类型 == "F" && b.成本细项ID == "KY"
                //                    select b;

                //    foreach (var myquery2 in myquery2s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery2.作业中心ID
                //                       select c).Count();
                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery2.作业中心ID;
                //            mynewrow.成本动因ID = 19;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);


                //        }


                //    }

                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }

                //}
                //#endregion

                //#region 冷冻进入各车间的水归集中心
                //if (myCenter.类型 == "A" && myCenter.成本细项ID == "LD")
                //{
                //    var myquery2s = from b in db.TABC_JS_CostCenter
                //                    where b.类型 == "F" && b.成本细项ID == "LD"
                //                    select b;

                //    foreach (var myquery2 in myquery2s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery2.作业中心ID
                //                       select c).Count();
                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery2.作业中心ID;
                //            mynewrow.成本动因ID = 19;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);


                //        }


                //    }

                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }

                //}
                //#endregion

                //#region 水中心向本层级作业中心分配水费
                //if (myCenter.类型 == "F" && myCenter.成本细项ID == "SF")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && new string[] { "BF", "BZ" }.Contains(b.类型)
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion


                //#region 水中心向本层级作业中心分配水费
                //if (myCenter.类型 == "F" && myCenter.成本细项ID == "DF")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && new string[] { "BF", "BZ" }.Contains(b.类型)
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion

                //#region 水中心向本层级作业中心分配水费
                //if (myCenter.类型 == "F" && myCenter.成本细项ID == "LD")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && new string[] { "BF", "BZ" }.Contains(b.类型)
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion

                //#region 水中心向本层级作业中心分配水费
                //if (myCenter.类型 == "F" && myCenter.成本细项ID == "KY")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && new string[] { "BF", "BZ" }.Contains(b.类型)
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion

                //#region 水中心向本层级作业中心分配水费
                //if (myCenter.类型 == "F" && myCenter.成本细项ID == "ZQ")
                //{
                //    var myquery1s = from b in db.TABC_JS_CostCenter
                //                    where b.上级作业中心ID == myCenter.上级作业中心ID && new string[] { "BF", "BZ" }.Contains(b.类型)
                //                    select b;
                //    foreach (var myquery1 in myquery1s)
                //    {
                //        var isexist = (from c in db.TABC_JS_CostCenterPathFramework
                //                       where c.作业中心ID == myCenter.作业中心ID &&
                //                       c.目标中心ID == myquery1.作业中心ID
                //                       select c).Count();

                //        if (isexist == 0)
                //        {
                //            TABC_JS_CostCenterPathFramework mynewrow = new TABC_JS_CostCenterPathFramework();
                //            mynewrow.公司ID = "hisun";
                //            mynewrow.账套ID = "001";
                //            mynewrow.作业中心ID = myCenter.作业中心ID;
                //            mynewrow.目标中心ID = myquery1.作业中心ID;
                //            mynewrow.成本动因ID = 14;
                //            db.TABC_JS_CostCenterPathFramework.InsertOnSubmit(mynewrow);

                //        }

                //    }
                //    try
                //    {
                //        db.SubmitChanges();

                //        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                //    }
                //    catch
                //    {

                //    }




                //}

                //#endregion

            //}
        }
        #endregion




        protected void Button_Query_Click(object sender, EventArgs e)
        {
            int Totals = TotalPageAmount();
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
            BindMyData(0);
        }


    }


}
